<@pp.dropOutputFile/>
<#include "../common/validation.ftl">
<#include "../common/util-easyui.ftl">

<#-- generate package name -->

<#-- iterate table list-->
<#list doc.tables as table>
<#assign module = doc.artifactid?replace(".", "/") + "/" + table.moduleid?uncap_first>
<#assign fileName = table.name?cap_first>
<#assign data_url = module + "/" + fileName>
<#if doc.tablePrefix?? && (doc.tablePrefix?length > 0)>
<#assign prefix = doc.tablePrefix + "_">
<#else>
<#assign prefix = "">
</#if>
<@pp.changeOutputFile name="/main/webapp/" + data_url + "/showitems_js.ftl"/>
<#noparse>
var $DT = [];
$(function(){
	$DT = $("#dataTable");
	init_toolbar();
	$DT.edatagrid({
		fit: true,
		nowrap: true,
		striped: true,
		url:"${base}/</#noparse>${data_url}/getPage.action",
		queryParams : array2map($("#search_form").serializeArray()),
		sortName: "obj.sys_update_time",
		sortOrder: "desc",
		remoteSort: false,
		idField:"obj.<#if table.idFieldName??>${table.idFieldName}<#else>id</#if>",
		singleSelect:true,
		checkOnSelect:true,
		selectOnCheck:true,
		pageSize : 20,
		pageList:[20, 50, 100],
		frozenColumns:[[
			{title:"ID",hidden:true, field:"obj.<#if table.idFieldName??>${table.idFieldName}<#else>id</#if>",width:40,sortable:true},
			{title:"ID",hidden:true, field:"obj.sys_update_time",width:40,sortable:true}
		]],
		columns:[[
			<#list table.fields as field>
			<#if field.name?lower_case != "id" && !field.name?starts_with("sys_")>
			{"field":"obj.${field.name}", "sortable":"true", "title":"${field.label}", "editor": ${getEditorValidator(field)}}<#if field_has_next>,</#if>
			</#if>
			</#list>
			<#noparse>
		]],
		updateUrl : "${base}/</#noparse>${data_url}<#noparse>/update.action",
		saveUrl : "${base}/</#noparse>${data_url}<#noparse>/create.action",
		destroyUrl : "${base}/</#noparse>${data_url}<#noparse>/delete.action",
		pagination : true,
		rownumbers : true,
		toolbar : "#toolbar",
		onLoadError:function(){
		},
		onLoadSuccess : function(data){
			//$(".button-edit").linkbutton({plain:true}).tooltip();
		}
	});
});
	
function init_toolbar(){
	$("#btn_add").bind('click', function(){$DT.edatagrid('addRow');});
	$("#btn_save").bind('click', function(){$DT.edatagrid('saveRow');});
	$("#btn_delete").bind('click', function(){$DT.edatagrid('destroyRow');});
	$("#btn_cancel").bind('click', function(){$DT.edatagrid('cancelRow');});
	$("#btn_copy").bind('click', doCopy);
	$("#btn_search").bind('click', doSearch);
	$("#btn_import").bind('click', doImport);
	$("#btn_export").bind('click', doExport);
	$("#btn_lock").bind('click', doLock);
	$("#btn_unlock").bind('click', doUnlock);
}

function doCopy(){
	var row = $DT.edatagrid('getSelected');
	if(row){
		row['obj.id'] = "";
		$DT.edatagrid('addRow',{
			row:row
		});
	}
}

function doLock(){
	ac(true);
}

function doUnlock(){
	ac(false);
}
function ac(lock){
	var rows = $("#dataTable").datagrid("getChecked");
	if(rows.length > 0){
		var items = {};
		for(var i=0;i<rows.length;i++){
			items["items[" + i + "]"] = rows[i]["obj.id"];
		}
		items["t"] = "</#noparse>${prefix}${fileName?upper_case}<#noparse>";
		$.post(lock ? "${base}/nm2016/ac/locks.action" : "${base}/nm2016/ac/unlock.action", 
			items, 
			function(data){
				if(data.success == 0){
					$("#dataTable").datagrid("reload");
				}
				showTip(data.success == 0, data.message);				
			},
			"json"
		);
	}
}

function doSearch(){
	$DT.edatagrid('load', array2map($("#search_form").serializeArray()));
}

function doImport(){
}

function doExport(){
	$.download("${base}/</#noparse>${data_url}<#noparse>/export.action", array2map($("#search_form").serializeArray()), "post");
}
</#noparse>
</#list>